<template>
  <header class="header">
    <div class="header-content">
      <div class="left-section">
        <button class="menu-btn" @click="$emit('toggle-history')" v-if="isAuthenticated">
          <i class="fas fa-bars"></i>
        </button>
        <div class="logo">
          <i class="fas fa-brain"></i>
          <span>DeepSeek Chat</span>
        </div>
      </div>
      
      <div class="center-section" v-if="isAuthenticated">
        <span class="current-chat-title">{{ currentChatTitle }}</span>
      </div>
      
      <div class="right-section" v-if="isAuthenticated">
        <div class="user-info">
          <span class="username">{{ user.username }}</span>
          <button class="logout-btn" @click="$emit('logout')">
            <i class="fas fa-sign-out-alt"></i>
            退出
          </button>
        </div>
      </div>

      <div class="right-section">
        <div class="user-info">
          <span class="username">{{ user.username }}</span>
          <button class="logout-btn" @click="handleLogout">
            <i class="fas fa-sign-out-alt"></i>
            退出
          </button>
        </div>
      </div>

    </div>
  </header>
</template>

<script>
import { auth } from '../utils/auth'

export default {
  name: 'Header',
  props: {
    currentChatTitle: {
      type: String,
      default: '新对话'
    },
    isAuthenticated: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      user: auth.getCurrentUser() || {}
    }
  },
  watch: {
    isAuthenticated: {
      immediate: true,
      handler(authenticated) {
        if (authenticated) {
          this.user = auth.getCurrentUser() || {};
        }
      }
    }
  },

  methods: {
    handleLogout() {
      auth.logout();
      this.$emit('logout');
    }
  }
}
</script>

<style scoped>
.header {
  background: rgba(15, 23, 42, 0.95);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid #334155;
  height: 60px;
  position: sticky;
  top: 0;
  z-index: 1000;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  height: 100%;
  max-width: 1200px;
  margin: 0 auto;
}

.left-section {
  display: flex;
  align-items: center;
  gap: 16px;
}

.menu-btn {
  background: none;
  border: none;
  color: #cbd5e1;
  font-size: 18px;
  cursor: pointer;
  padding: 8px;
  border-radius: 6px;
  transition: background 0.3s;
}

.menu-btn:hover {
  background: rgba(96, 165, 250, 0.1);
}

.logo {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 18px;
  font-weight: 700;
  color: #60a5fa;
}

.center-section {
  flex: 1;
  text-align: center;
  padding: 0 20px;
}

.current-chat-title {
  font-size: 16px;
  font-weight: 500;
  color: #e2e8f0;
  background: rgba(96, 165, 250, 0.1);
  padding: 6px 16px;
  border-radius: 20px;
  border: 1px solid rgba(96, 165, 250, 0.2);
}

.right-section {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.username {
  color: #cbd5e1;
  font-size: 14px;
  background: rgba(51, 65, 85, 0.3);
  padding: 4px 12px;
  border-radius: 12px;
}

.logout-btn {
  background: rgba(239, 68, 68, 0.1);
  border: 1px solid rgba(239, 68, 68, 0.2);
  color: #ef4444;
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  gap: 4px;
}

.logout-btn:hover {
  background: rgba(239, 68, 68, 0.2);
}

@media (max-width: 768px) {
  .header-content {
    padding: 0 16px;
  }
  
  .logo span {
    display: none;
  }
  
  .username {
    display: none;
  }
  
  .current-chat-title {
    font-size: 14px;
    padding: 4px 12px;
  }
}
</style>